package org.apache.shiro.web.validator;

import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;

public class ChangGengLoginValidator extends LoginValidator {
    private String key;
    private String iv;//加密解密向量
    private String token;
    private String username;

    public ChangGengLoginValidator() {
    }

    public ChangGengLoginValidator(HttpServletRequest httpRequest) {
        this.token = httpRequest.getParameter("token");
    }

    @Override
    public boolean hasRequiredParam(HttpServletRequest httpRequest) {
        this.token = httpRequest.getParameter("token");
        System.out.println(this.token);
        if (this.token != null) {
            this.token = this.token.replaceAll("\\s", "+");
            System.out.println(this.token);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean isValidate() {
        Base64 base64 = new Base64();

        try {
            String text = new String(decrypt(base64.decode(this.token), base64.decode(this.key), base64.decode(this.iv)), "UTF-8");
            Map<String, String> map = getParamMap(text);
            if ("DRG".equals(map.get("app")) && map.get("username") != null) {
                this.username = map.get("username");
                System.out.println("ChangGeng单点登录");
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    //解密
    public static byte[] decrypt(byte[] data, byte[] key, byte[] iv) throws Exception {
        SecretKey secretKey = new SecretKeySpec(key, "TripleDES");
        IvParameterSpec secretIv = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, secretIv);
        byte[] plainBytes = cipher.doFinal(data);
        return plainBytes;
    }

    //加密
    public static byte[] encrypt(byte[] data, byte[] key, byte[] iv) throws Exception {
        SecretKey secretKey = new SecretKeySpec(key, "TripleDES");
        IvParameterSpec secretIv = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, secretIv);

        byte[] plainBytes = cipher.doFinal(data);
        return plainBytes;
    }

    //解析查询字符串
    public static Map<String, String> getParamMap(String queryString) {
        Map<String, String> map = new HashMap();
        if (queryString != null) {
            String[] keyValueStr = queryString.split("&");
            String[] var7 = keyValueStr;
            int var6 = keyValueStr.length;

            for (int var5 = 0; var5 < var6; ++var5) {
                String kv = var7[var5];
                if (kv != null) {
                    map.put(kv.split("=")[0], kv.split("=")[1]);
                }
            }
        }

        return map;
    }

    public String getKey() {
        return this.key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getIv() {
        return this.iv;
    }

    public void setIv(String iv) {
        this.iv = iv;
    }

    //测试加密解密
    public static void main(String[] args){
        String key = "dHNpbmdodWFfaGlzX2RyZ19wYXNza2V5";
        String iv = "cGFzc19kcmc=";
        String textOrign = "app=DRG&username=yizheng";
        String token = "bBUzQsLGywghuIhNwX/20/+ZxfAU+Zq6irO8D8oSnFAg56nr5RipvT3hTIzqhkx4rDiWHGhrmMuKoCH36M6RZAigLc9g/F4WNytiUEpTq9CFkEOcKA669G7QZ+YXbfGFkWO3hf22t7gB/ibHtSMCsxEFd1NlrdFy";
        Base64 base64 = new Base64();
        try {
            token = base64.encodeToString(encrypt(textOrign.getBytes("UTF-8"), base64.decode(key), base64.decode(iv)));
            System.out.println(token);
            String text = new String(decrypt(base64.decode(token), base64.decode(key), base64.decode(iv)),"UTF-8");
            System.out.println(text);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
